import vue from '@vitejs/plugin-vue';
import { resolve } from 'node:path';
import AutoImport from 'unplugin-auto-import/vite';
import VueSetupExtend from 'vite-plugin-vue-setup-extend';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
import { defineConfig, loadEnv } from 'vite';
import { wrapperEnv } from './build/utils';
import { createProxy } from './build/vite/proxy';
import UnoCSS from 'unocss/vite'

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}
console.log(resolve(__dirname,'./'))

export default defineConfig(({ command, mode }) => {
  console.log('当前环境: ', command);
  const root = process.cwd();
  const env = loadEnv(mode, root);

  // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  const viteEnv = wrapperEnv(env)

  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
  return {
		base: './',
    // dev 配置 
    overlay: false,
    define: {
      '__APP_VERSION__': JSON.stringify(process.env.npm_package_version),
    },
    plugins: [vue(),
      VueSetupExtend(),
      
      AutoImport({
        resolvers: [ElementPlusResolver()],
        // 自动导入vue组件,函数
        imports: ['vue','vue-router','pinia'],
        eslintrc: {
          enabled: true,
          globalsPropValue: true
        }
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      }),
      UnoCSS(),
    ],
    resolve: {
      extensions: ['.js', '.vue', '.json','.ts','.d.ts'], // 引用文件省去后缀
      alias: {
        // 设置别名
        '@': resolve(__dirname, './src'),
        'types': resolve(__dirname, './types')
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          // additionalData: `@use "src/assets/scss/index.scss" as *;`
        }
      },
      postcss: {
        plugins:  [require("autoprefixer")]
      }
    },
    server: {
      host: true,
      port: VITE_PORT,
      https: false,
      logLevel:'debug',
      proxy:  createProxy(VITE_PROXY)
    },
    build: {
      target: 'esnext'
    }
  }

})